<?php

namespace fast;



use think\Db;
use addons\shopro\library\commission\Config;

use think\Exception;
use think\Cache;
use addons\shopro\model\User;

/**
 * Http 请求类
 */
class Tongbu
{
    const GET_AUTH_CODE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize";
    const GET_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo";
    const GET_ACCESS_TOKEN_CODE_URL = "https://api.weixin.qq.com/cgi-bin/token";
    public $UserId;
    public $Password;
    public $token;
  
    /**
     * 构造函数
     * @param $sessionKey string 用户在小程序登录后获取的会话密钥
     * @param $appid string 小程序的appid
     */
    public function __construct()
    {   
       

        $this->UserId = 'A0001';
        $this->Password = 'C8DFFC722F39C39744262B8B7BF4D560';
        $this->token = Cache::get('crm_token');
    
        if (empty($this->token)) {
            $this->getToken();
        }
    }


    //获取token
    public function getToken(){
        $url = 'http://dongd.imyerp.com/Service/Service.svc/Authorization?UserId='.$this->UserId.'&Password='.$this->Password;
        
        $data = Http::get($url);

        $data = json_decode($data,true);
        if (isset($data['Success']) && $data['Success'] == true) {
            $this->token = $data['Data'];
            Cache::set('crm_token',$data['Data'],7200);
        }

    }

    //获取账套ID
    public function getAccounts(){
        $url = 'http://dongd.imyerp.com/Service/Service.svc/Accounts';
        
        $headers = ['Content-type: application/json',"Authorization:".$this->token];

        $options = [

            CURLOPT_HTTPHEADER => $headers

        ];


        $data = Http::get($url,[],$options);

        $data = json_decode($data,true);

       
        

    }

    public function createOrder($orderId){
        $url = 'http://dongd.imyerp.com/Service/Service.svc/Order/News';
        
        $arealist = getarea();
        $order = Db::table('fa_pifa_order')->find($orderId);
        $Orderitem = new \app\admin\model\pifa\Orderitem;
        $map['fa_pifa_orderitem.order_id'] = $orderId;
        $goods_item = $Orderitem->where($map)->select();

        $user = Db::table('fa_user')->find($order['user_id']);
     
        $post_params['AccountId'] = 5;
        $post_params['CustNo'] = $order['order_no'];
        $post_params['NCode'] = $user['third_code'];
        $post_params['Contacts'] = $order['consignee'];
        $post_params['Phone'] = $order['phone'];
        $post_params['Province'] = isset($arealist[$order['province_id']]) ? $arealist[$order['province_id']]['name'] : '';
        $post_params['City'] = isset($arealist[$order['city_id']]) ? $arealist[$order['city_id']]['name'] : '';
        $post_params['County'] = isset($arealist[$order['area_id']]) ? $arealist[$order['area_id']]['name'] : '';
        $post_params['Address'] = $order['address'];
        $post_params['Remark'] = $order['emo'];
        $post_params['Source'] = '小程序商城';
        $post_params['PaymentAmount'] = $order['price'];
        $post_params['Items'] = [];

        foreach ($goods_item as $key => $value) {
            $post_params['Items'][$key] = [];
            $post_params['Items'][$key]['ProductCode'] = $value['goods_no'];
            $post_params['Items'][$key]['Qty'] = $value['goods_num'];
        }


        $headers = ['Content-type: application/json',"Authorization:".$this->token];

        $options = [

            CURLOPT_HTTPHEADER => $headers

        ];

        $post_params = json_encode($post_params,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

       
        logs('tonbu_res','请求参数----------'.$post_params);

        $data = Http::post($url,$post_params,$options);
        
        $data = json_decode($data);
        
        logs('tonbu_res','请求返回----------'.json_encode($data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
        return $data;
    }


    public function getuserphonenumber($code){
        $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$this->token;
        
        $data['code'] = $code;
        $data = json_encode($data);

    
        $ret = Http::post($url,$data);
        $ret = html_entity_decode($ret);
        $ret = json_decode($ret,true);
        return $ret;
    }


  
}
